package com.diandiansong.app.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.diandiansong.app.R;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.ui.cart.CartMain;
import com.diandiansong.app.ui.cate.Cate;
import com.diandiansong.app.ui.commonlist.CommonList;
import com.diandiansong.app.ui.index.IndexMain;
import com.diandiansong.app.ui.user.UserMain;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 用户端
 * 主页
 */

public class Main extends BaseAct {

    public static void start(Context context) {
        Intent starter = new Intent(context, Main.class);
        context.startActivity(starter);
    }

    @BindView(R.id.rg_tab)
    RadioGroup rgTab;
    @BindView(R.id.view_main_notity_new)
    View mNotity;

    private List<Fragment> mFragments = new ArrayList<>();
    private FragmentManager mFragmentManager;
    private String[] mFragmentNames = new String[]{
            IndexMain.class.getName(),
            Cate.class.getName(),
            Fragment.class.getName(),
            Fragment.class.getName(),
            UserMain.class.getName(),
    };

    private Bundle[] mBundles = new Bundle[]{
            null,
            null,
            null,
            null,
            null,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.main_tab);
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void init() {
        mNotity.setVisibility(View.INVISIBLE);

        mFragmentManager = getSupportFragmentManager();

        {
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            //实例化第一个页面
            for (int i = 0; i < mFragmentNames.length; i++) {
                if (i == 0) {
                    mFragments.add(Fragment.instantiate(getActivity(), mFragmentNames[i], mBundles[i]));
                } else {
                    mFragments.add(null);
                }
            }
            fragmentTransaction.add(R.id.main_container, mFragments.get(0));
            fragmentTransaction.commit();
        }

        rgTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                int index = 0;
                switch (arg1) {
                    case R.id.rb_main_1:
                        hideTitle();
                        IndexMain.doRefresh();
                        break;

                    case R.id.rb_main_2:
                        index = 1;
                        hideTitle();
                        break;

                    case R.id.rb_main_3:
                        index = 2;
                        hideBack();
                        //购物车;
                        break;

                    case R.id.rb_main_4:
                        index = 3;
                        hideTitle();
                        break;

                    case R.id.rb_main_5:
                        index = 4;
                        hideTitle();
                        break;
                }
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                for (int i = 0; i < mFragments.size(); i++) {
                    Fragment hideFragment = mFragments.get(i);
                    if (hideFragment != null) {
                        fragmentTransaction.hide(mFragments.get(i));
                    }
                }
                Fragment showFragment = mFragments.get(index);
                if (showFragment == null) {
                    showFragment = Fragment.instantiate(getActivity(), mFragmentNames[index], mBundles[index]);
                    mFragments.remove(index);
                    mFragments.add(index, showFragment);
                    fragmentTransaction.add(R.id.main_container, showFragment);
                    fragmentTransaction.commit();

                } else {
                    fragmentTransaction.show(showFragment);
                    fragmentTransaction.commitAllowingStateLoss();
                }
                if (index == 4) {//等页面加载后再刷新
                    rgTab.postDelayed(new Runnable() {
                        @Override
                        public void run() {

                        }
                    }, 500);
                }
            }
        });

        rgTab.check(R.id.rb_main_1);
    }

    public static void index(int index) {
        EventBus.getDefault().post(new Index(index));
    }

    @OnClick(R.id.rb_main_3)
    public void onMRbMain3Clicked() {
        CartMain.start(getActivity());
    }

    @OnClick(R.id.rb_main_4)
    public void onMRbMain4Clicked() {
        CommonList.start(getActivity());
    }

    public static class Index {
        public int index;

        public Index(int index) {
            this.index = index;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void goIndex(Index index) {
        int rid = R.id.rb_main_1;
        switch (index.index) {
            case 0:
                rid = R.id.rb_main_1;
                break;
            case 1:
                rid = R.id.rb_main_2;
                break;
            case 2:
                rid = R.id.rb_main_3;
                break;
            case 3:
                rid = R.id.rb_main_4;
                break;
            case 4:
                rid = R.id.rb_main_5;
                break;
        }
        rgTab.check(rid);
    }

    public static class Finish {
    }

    public static void doFinish() {
        EventBus.getDefault().post(new Finish());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveFinish(Finish finish) {
        finish();
    }

}